package com.uni.clash;

import android.app.Activity;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.sunmi.printerx.PrinterSdk;
import com.sunmi.printerx.SdkException;
import com.sunmi.printerx.api.PrintResult;

import java.util.List;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniDestroyableModule;

public class ClashMoudle extends UniDestroyableModule {

    public PrinterSdk.Printer selectPrinter;
    /**
     * 初始化默认打印机作为待操作的打印设备
     */
    @UniJSMethod(uiThread = true)
    public void initPrinter(JSONObject options, UniJSCallback jsCallback) {
        if (mUniSDKInstance.getContext() instanceof Activity) {
            try {
                Log.d("wdx", "开始获取context");
                Context context = this.mUniSDKInstance.getContext();
                Log.d("wdx", "context " + context.getPackageName());
                PrinterSdk printerSdk = PrinterSdk.getInstance();
                Log.d("wdx", "printerSdk " + printerSdk.toString());
                printerSdk.getPrinter(context, new PrinterSdk.PrinterListen() {
                    @Override
                    public void onDefPrinter(PrinterSdk.Printer printer) {
                        Log.d("wdx", "初始化成功 " + printer.toString());
                        selectPrinter = printer;
                        open(selectPrinter, jsCallback);
                        JSONObject result = new JSONObject();
                        result.put("result","初始化成功");
                        jsCallback.invokeAndKeepAlive(result);
                    }

                    @Override
                    public void onPrinters(List<PrinterSdk.Printer> printers) {
                        Log.d("wdx", "初始化成功 " + printers.get(0).toString() + "  数量 " + printers.size());
                        selectPrinter = printers.get(0);
                        open(selectPrinter, jsCallback);
                        JSONObject result = new JSONObject();
                        result.put("result","初始化成功 有几个： " + printers.size());
                        jsCallback.invokeAndKeepAlive(result);
                    }
                });
                Log.d("wdx", "等待初始化回调 ");
                JSONObject result = new JSONObject();
                result.put("result","等待初始化 ");
                jsCallback.invokeAndKeepAlive(result);
            } catch (SdkException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 打开钱箱
     */
    @UniJSMethod(uiThread = true)
    public void open(JSONObject options, UniJSCallback jsCallback){
        if (mUniSDKInstance.getContext() instanceof Activity) {
            //打开钱箱1
            try {
                Log.d("wdx", "打开钱箱");
                selectPrinter.cashDrawerApi().open(new PrintResult() {
                    @Override
                    public void onResult(int i, String s) throws RemoteException {
//                    tvCashStatus.setText("resultCode: " + i + " message: " + s);
//                        result.result(i, s);
                        Log.d("wdx", "打开钱箱成功");
                        JSONObject result = new JSONObject();
                        result.put("result","resultCode: " + i + " message: " + s);
                        jsCallback.invokeAndKeepAlive(result);
                    }
                });
                Log.d("wdx", "等待打开钱箱");
            } catch (SdkException e) {
                e.printStackTrace();
            }
        }

    }

    public void open(PrinterSdk.Printer printer, UniJSCallback jsCallback){
        if (mUniSDKInstance.getContext() instanceof Activity) {
            //打开钱箱1
            try {
                Log.d("wdx", "打开钱箱");
                printer.cashDrawerApi().open(new PrintResult() {
                    @Override
                    public void onResult(int i, String s) throws RemoteException {
//                    tvCashStatus.setText("resultCode: " + i + " message: " + s);
//                        result.result(i, s);
                        Log.d("wdx", "打开钱箱成功");
                        JSONObject result = new JSONObject();
                        result.put("result","resultCode: " + i + " message: " + s);
                        jsCallback.invokeAndKeepAlive(result);
                    }
                });
                Log.d("wdx", "等待打开钱箱");
            } catch (SdkException e) {
                e.printStackTrace();
            }
        }

    }

    public interface openResult{
        void result(int i, String s);
    }

    @Override
    public void destroy() {
        PrinterSdk.getInstance().destroy();
    }
}
